<?php
declare(strict_types=1);
namespace App\Model\CommodityModel;

use App\Model\BaseModel\BaseCommodityModel;
use Hyperf\DbConnection\Db;
class SupplierCompany extends BaseCommodityModel
{
    public $table = 'suppliercompany_s2b';
    protected $casts = [
        'ID' => 'string',
    ];
    
    /**
     * 批量获取供应商信息
     * @param array $companyIds
     */
    public function getSupplierListByIds( $ids = [] )
    {
        $supplierList = $this->
        select([
            Db::raw("CONCAT(s.ID,'') AS ID"),
            "s.OrderDeclare",
            "s.PayDeclare",
            "s.OrderDockMode",
            "s.SupplierCode",
            "s.SupplierName",
            "s.thirdPartyPlatformCode",
            Db::raw("IFNULL(w.JsonBody,'') AS JsonBody"),
        ])
        ->from($this->table. " as s")
        ->join("warehouseinfo_s2b as w","w.SupplierID","=","s.ID")
        ->whereIn("s.ID",$ids)
        ->where("w.ExamineState",1)
        ->get()
        ->toArray();
        $companyList  = [];
        foreach ($supplierList as $item )
        {
            $JsonBody = @json_decode($item['JsonBody'],true);
            $JsonBody = $JsonBody ? $JsonBody : [];
            $company_code = !empty($item['thirdPartyPlatformCode']) ? $item['thirdPartyPlatformCode'] : $item['SupplierCode'] ;
            $companyList[] = [
                "company_id"    => (string)$item['ID'],
                "OrderDeclare"  => (string)$item['OrderDeclare'],
                "PayDeclare"    => (string)$item['PayDeclare'],
                "OrderDockMode" => (string)$item['OrderDockMode'],
                "company_code"  => (string)$company_code,
                "company_name"  => (string)$item['SupplierName'],
                "apiBody"       => $JsonBody 
            ];
            unset($item);
        }
        return $companyList;
    }

    /**
     * 获取单个供应商信息
     * @param string $id
     */
    public function getSupplierListById( $id )
    {
        $supplierList = $this->
        select([
            Db::raw("CONCAT(s.ID,'') AS ID"),
            "s.OrderDeclare",
            "s.PayDeclare",
            "s.OrderDockMode",
            "s.SupplierCode",
            "s.SupplierName",
            "s.thirdPartyPlatformCode",
            Db::raw("IFNULL(w.JsonBody,'') AS JsonBody"),
        ])
        ->from($this->table. " as s")
        ->join("warehouseinfo_s2b as w","w.SupplierID","=","s.ID")
        ->where("s.ID",$id)
        ->where("w.ExamineState",1)
        ->first()
        ->toArray();
        $JsonBody = @json_decode($supplierList['JsonBody'],true);
        $supplierList["JsonBody"] = $JsonBody ? $JsonBody : [];
        $supplierList["company_code"] = !empty($supplierList['thirdPartyPlatformCode']) ? $supplierList['thirdPartyPlatformCode'] : $supplierList['SupplierCode'] ;
        return $supplierList;
    }
    /**
     * 获取单个供应商信息
     * @param string $SupplierCode
     */
    public function getSupplierListBySupplierCode( $SupplierCode )
    {
        $supplierList = $this->
        select([
            Db::raw("CONCAT(s.ID,'') AS ID"),
            "s.OrderDeclare",
            "s.PayDeclare",
            "s.OrderDockMode",
            "s.SupplierCode",
            "s.SupplierName",
            "s.thirdPartyPlatformCode",
            Db::raw("IFNULL(w.JsonBody,'') AS JsonBody"),
        ])
        ->from($this->table. " as s")
        ->join("warehouseinfo_s2b as w","w.SupplierID","=","s.ID")
        ->where("s.SupplierCode",$SupplierCode)
        ->where("w.ExamineState",1)
        ->first()
        ->toArray();
        $JsonBody = @json_decode($supplierList['JsonBody'],true);
        $supplierList["JsonBody"] = $JsonBody ? $JsonBody : [];
        $supplierList["company_code"] = !empty($supplierList['thirdPartyPlatformCode']) ? $supplierList['thirdPartyPlatformCode'] : $supplierList['SupplierCode'] ;
        return $supplierList;
    }
}